package com.weigee.ktv.view.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.linde.library.util.DisplayUtil
import com.linde.library.util.ListUtil
import com.weigee.ktv.R
import com.weigee.ktv.presenter.AllSupermarketPresenter
import com.weigee.ktv.view.dialog.LoadingDialog
import com.weigee.ktv.view.interfaces.AllSupermarketView
import kotlinx.android.synthetic.main.include_status_bar_height.*
import kotlinx.android.synthetic.main.include_title_bar.*
import kotlinx.android.synthetic.main.wrap_recycler_refresh.*

/**
 * Created by linde on 2017/6/13.
 * LinDe
 */
class AllSupermarketFragment : BaseFragment(), AllSupermarketView
{
    private var presenter: AllSupermarketPresenter? = null
    private var loadingDialog: LoadingDialog? = null

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        presenter = AllSupermarketPresenter(this)
        presenter?.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        return inflater?.inflate(R.layout.activity_all_supermarket, container, false)
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?)
    {
        super.onViewCreated(view, savedInstanceState)
        DisplayUtil.setStatusBarHeight(linear_status_bar_height)

        my_title_bar.leftIconClickBack = activity
        my_title_bar.title = "超市"

        ListUtil.initRecyclerView(recycler_view, presenter?.adapter)
        ListUtil.initTwinklingRefreshLayout(refresh_Layout, object : RefreshListenerAdapter()
        {
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?)
            {
                super.onRefresh(refreshLayout)
                presenter?.loadDataList(false)
                refreshLayout?.finishRefreshing()
            }

            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?)
            {
                super.onLoadMore(refreshLayout)
                presenter?.loadDataList(false)
            }
        })
    }

    override fun taskPreview()
    {
        loadingDialog?.dismiss()
        loadingDialog = LoadingDialog()
        loadingDialog?.show(childFragmentManager, javaClass.name)
    }

    override fun taskCompletely()
    {
        loadingDialog?.dismiss()
    }

    override fun onLoadDataCompletely(moreData: Boolean)
    {
        if (moreData) refresh_Layout.finishLoadmore() else refresh_Layout.finishRefreshing()
    }
}